<?php
declare (strict_types=1);

namespace app\api\controller\user;

use app\api\controller\Base;
use app\common\service\user\UserCollectionService;
use think\facade\Cache;
use think\Request;

class UserCollection extends Base
{
    /**
     * Author:Zhongyuan Lu
     * 收藏商品
     * @return mixed|void
     */
    public function collection()
    {
        if (!$this->request->isGet()) {
            return $this->result([], 400, '请求方式错误');
        }

        $product_id = $this->request->get('goods_id', 0, 'int');
        $uid = $this->request->uid;
        if (empty($product_id) || $product_id < 1) {
            return $this->result([], 400, '参数错误');
        }

        $data = UserCollectionService::collection($uid,$product_id);
        $redis = Cache::store('redis');
        $redis->select(1);
        if ($data['message'] == '收藏成功') {
            $redis->set("one:ea_user_collection:uid{$uid}:product_id{$product_id}",time());
        }
        if ($data['message'] == '取消收藏') {
            $redis->delete("one:ea_user_collection:uid{$uid}:product_id{$product_id}");
        }
        return $this->res($data);
    }

    /**
     * Author:Yingbo Sun
     * 我的收藏
     * @return mixed
     */
    public function collectionList()
    {
        $uid = $this->request->uid;
//        $uid = 1;
        if (empty($uid)){
            $data = ['code' => 400,'message' => '参数错误'];
            return $this->res($data);
        }
        $data = UserCollectionService::collectionList($uid);
        return $this->res($data);
    }
}
